package com.xbq.exceleditor.ui

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.xbq.exceleditor.R
import com.xbq.exceleditor.adapter.ProductAdapter
import com.xbq.exceleditor.bean.model.ProductItem
import com.xbq.exceleditor.databinding.ActivityBuyvipBinding
import com.xbq.exceleditor.databinding.DlgBuyVipFailedBinding
import com.xbq.exceleditor.utils.formatFeatures
import com.xbq.exceleditor.utils.isVip
import com.xbq.xbqsdk.XbqSdk
import com.xbq.xbqsdk.core.pay.ChoosePayTypeDialog
import com.xbq.xbqsdk.core.pay.WxEnvChecker
import com.xbq.xbqsdk.core.ui.product.vip.XbqBaseVipActivity
import com.xbq.xbqsdk.net.common.dto.ProductListDto
import com.xbq.xbqsdk.net.userCache
import com.xbq.xbqsdk.util.click.onDebouncedClick
import com.xbq.xbqsdk.util.coroutine.launch
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * Author: liaohaiping
 * Time: 2020-10-30
 * Description:
 */
@AndroidEntryPoint
class BuyVipActivity : XbqBaseVipActivity() {
    @Inject
    lateinit var wxEnvChcker: WxEnvChecker
    @Inject
    lateinit var adapter:ProductAdapter

    lateinit var binding:ActivityBuyvipBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBuyvipBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            fitsSystemWindows(true,R.color.buy_vip_bg)
        }
        binding.btnBack.onDebouncedClick { onBackPressed() }
        binding.tvVipAgreement.onDebouncedClick { XbqSdk.startVipAgreement(this) }
        binding.btnBuyVip.onDebouncedClick { buyVipClick() }
        binding.recycleView.layoutManager = LinearLayoutManager(this)
        binding.recycleView.adapter = adapter
        adapter.setOnItemClickListener { d, view, position ->
            adapter.data.forEach { it.checked = false }
            adapter.getItem(position).checked = true
            adapter.notifyDataSetChanged()
        }
        if (userCache.isVip()) {
            binding.tvCurrentVipStatus.text = userCache.getLoginData()?.formatFeatures()?:""
        } else {
            binding.tvCurrentVipStatus.setText(R.string.be_vip_will_unlock_more_features)
        }
        loadProducts()
    }

    fun buyVipClick() {
        ChoosePayTypeDialog(wxEnvChcker)
            .autoShowDoublePayButtonVisibility()
            .setPayClickListener{
                val product = adapter.selectedProduct()
                if (product==null){
                    ToastUtils.showShort("请选择产品")
                    return@setPayClickListener
                }
                doPay(product,it)
            }
            .show()
    }


    override fun onBackPressed() {
        val dlgBinding = DlgBuyVipFailedBinding.inflate(layoutInflater, null, false)
        val alertDialog = AlertDialog.Builder(this, R.style.TransparentDialog)
            .setView(dlgBinding.root)
            .setCancelable(false)
            .create()

        dlgBinding.btnClose.onDebouncedClick {
            alertDialog.dismiss()
            finish()
        }
        dlgBinding.btnUnlockOnStep.onDebouncedClick { alertDialog.dismiss() }
        alertDialog.show()
        val param = alertDialog.window?.attributes?.apply {
            width = SizeUtils.dp2px(300f)
            height = SizeUtils.dp2px(300f)
        }
        alertDialog.window?.attributes = param

    }


    private fun loadProducts() {
        launch {
            val res = commonApi.productList(ProductListDto(feature!!))
            if (res.success()) {
                val list = res.data!!.mapIndexed { index, it ->
                    ProductItem(it, index, index == 0)
                }
                adapter.setList(list)
            } else {
                ToastUtils.showShort(res.message)
            }
        }
    }
    companion object{
        fun newIntent(context: Context,feature:String):Intent{
            return Intent(context,BuyVipActivity::class.java).apply {
                putExtra("feature",feature)
            }
        }
    }
}